'use strict';

const Controller = require('egg').Controller;

class SubscribeController extends Controller {

  /**
   * 新增订阅接口
   */
  async addSubscribe() {
    const { ctx } = this;
    ctx.body = ctx.request.body;// post json

    let retrunMap = {};
    try {
      const subscribe = await this.ctx.service.subscribeService.addSubscribe(ctx.body);

      if (subscribe) {

        retrunMap = { code: '1', msg: '订阅成功' };
      } else {
        retrunMap = { code: '0', msg: '订阅失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '订阅失败' };
      console.log(err);
    }
    ctx.body = retrunMap;

  }

  /**
   * 取消订阅
   */
  async cancelSub() {
    const { ctx } = this;
    ctx.body = ctx.request.body;// post json
    let retrunMap = {};
    try {
      const subscribe = await this.ctx.service.subscribeService.cancelSub(ctx.body);

      if (subscribe) {

        retrunMap = { code: '1', msg: '取消订阅成功' };
      } else {
        retrunMap = { code: '0', msg: '取消订阅失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '取消订阅失败' };
      console.log(err);
    }
    ctx.body = retrunMap;

  }

}

module.exports = SubscribeController;
